package com.woke.method;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.TextUtils;
import android.util.Log;

import com.loopj.android.http.PersistentCookieStore;

import org.apache.http.cookie.Cookie;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Enumeration;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Intentnet {
	
	/** 
     * ��ȡ��׼ Cookie   
     */  
    public List<Cookie> getCookieText(Context con) {  
        PersistentCookieStore myCookieStore = new PersistentCookieStore(con);  
        List<Cookie> cookies = myCookieStore.getCookies();  
        Log.e("cookies.size() = " ,"  "+ cookies.size());  
        Log.e("cookies.size() = " ,"  "+ cookies);  
//        Util.setCookies(cookies);  
        for (Cookie cookie : cookies) {  
            Log.e("Util", cookie.getName() + " = " + cookie.getValue());  
        }  
         StringBuffer sb = new StringBuffer();  
        for (int i = 0; i < cookies.size(); i++) {  
             Cookie cookie = cookies.get(i);  
             String cookieName = cookie.getName();  
             String cookieValue = cookie.getValue();  
            if (!TextUtils.isEmpty(cookieName)  
                    && !TextUtils.isEmpty(cookieValue)) {  
                sb.append(cookieName + "=");  
                sb.append(cookieValue + ";");  
            }  
        }  
        Log.e("cookie", sb.toString());  
        return cookies;  
    }  
    
    /**
	 * 
	 * �绰��ʾ
	 * 
	 * */
	public static String getTel_no(String tel_no) {
		String s = "***********";
		if (tel_no.length() < 8) {
			tel_no = tel_no.substring(0, 3)
					+ s.substring(0, tel_no.length() - 3);
			return tel_no;
		}
		String tel_midd = tel_no.substring(3, tel_no.length() - 3);
		tel_no = tel_no.replaceAll(tel_midd, s.substring(0, tel_midd.length()));
		return tel_no;

	}
    
 // ��ʱ���תΪ�ַ�
    public static String getStrTime(String cc_time) {
    String re_StrTime = null;

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时");
    // ���磺cc_time=1291778220
    long lcc_time = Long.valueOf(cc_time);
    re_StrTime = sdf.format(new Date(lcc_time * 1000L));

    return re_StrTime;

    }
    
 // ��ʱ���תΪ�ַ�
    public static String getStrTime1(String cc_time) {
    String re_StrTime = null;

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
    // ���磺cc_time=1291778220
    long lcc_time = Long.valueOf(cc_time);
    re_StrTime = sdf.format(new Date(lcc_time * 1000L));

    return re_StrTime;

    }
    
    public static String getStrTi(String cc_time) {
        String re_StrTime = null;

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // ���磺cc_time=1291778220
        long lcc_time = Long.valueOf(cc_time);
        re_StrTime = sdf.format(new Date(lcc_time * 1000L));

        return re_StrTime;

        }
    
 // ���ַ�תΪʱ���

    public static String getTime(String user_time) {
    String re_time = null;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
    java.util.Date d;
    try {

    d = sdf.parse(user_time);
    long l = d.getTime();
    String str = String.valueOf(l);
    re_time = str.substring(0, 10);

    } catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return re_time;
    }
    
 // ���ַ�תΪʱ���

    /** 
     * ��ip��������ʽת����ip��ʽ 
     *  
     * @param ipInt 
     * @return 
     */  
    public static String int2ip(int ipInt) {  
        StringBuilder sb = new StringBuilder();  
        sb.append(ipInt & 0xFF).append(".");  
        sb.append((ipInt >> 8) & 0xFF).append(".");  
        sb.append((ipInt >> 16) & 0xFF).append(".");  
        sb.append((ipInt >> 24) & 0xFF);  
        return sb.toString();  
    }  
  
    public static int getcurrenttime() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");       
		Date curDate = new Date(System.currentTimeMillis());//��ȡ��ǰʱ��       
		String strtime = formatter.format(curDate);
		String timejls = Intentnet.getTime(strtime);
		int timejlss = Integer.parseInt(timejls);
		Log.e("str", "str"+timejls);
		return timejlss;
	}
    
    public static String getcurrenttime1() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");      
		Date curDate = new Date(System.currentTimeMillis());//��ȡ��ǰʱ��       
		String strtime = formatter.format(curDate);
		String timejls = Intentnet.getTime(strtime);
		int timejlss = Integer.parseInt(timejls);
		Log.e("str", "str"+timejls);
		return strtime;
	}
    
    /** 
     * ������ȡ�ֻ���������CTWAP��CTNET��ʱ��PDSN������ֻ��ն˵�ԴIP��ַ�� 
     *  
     * @return 
     * @author SHANHY 
     */  
    public static String getPsdnIp() {  
        try {  
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {  
                NetworkInterface intf = en.nextElement();  
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {  
                    InetAddress inetAddress = enumIpAddr.nextElement();  
                    if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {  
                    //if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet6Address) {  
                        return inetAddress.getHostAddress().toString();  
                    }  
                }  
            }  
        } catch (Exception e) {  
        }  
        return "";  
    }  
    
//    public static String uploadmap(){
//    String _strUrl = "";
//	_strUrl = _strUrl.replace(" ", "%20");
//	URL url;
//	DataOutputStream req = null;
//	try {
//		url = new URL(_strUrl);
//		// trustAllHosts();
//		conn = (HttpURLConnection) url
//				.openConnection();
//		conn.setDoInput(true);
//		conn.setDoOutput(true);
//		conn.setConnectTimeout(TIMEOUT_CONNECTION);
//		conn.setReadTimeout(TIMEOUT_SOCKET);
//		conn.setUseCaches(false);
//		conn.setRequestMethod("POST");
//		conn.setRequestProperty("content-type", "text/html");
//		conn.connect();
//		byte[] imageByte = protocol.imageByte();
//		if (imageByte != null) {
//			req = new DataOutputStream(conn.getOutputStream());
//			req.write(imageByte);
//			req.flush();
//			req.close();
//		}
//		if (conn.getResponseCode() == HttpsURLConnection.HTTP_OK) {
//			isSuccess = true;
//			BufferedInputStream read = new BufferedInputStream(
//					conn.getInputStream(), 4000);
//			// �����
//			ByteArrayOutputStream write = new ByteArrayOutputStream();
//			BufferedOutputStream out = new BufferedOutputStream(
//					write, 4000);
//			int readLen = -1;
//			byte[] buffer = new byte[4000];
//			while ((readLen = read.read(buffer)) > 0) {
//				out.write(buffer, 0, readLen);
//			}
//			read.close();
//			out.flush();
//			out.close();
//			byte[] readBuf = write.toByteArray();
//			String result = new String(readBuf, code);
//			strResult = result.trim();
//		}
//    
//	}
//    }
    /** 
	 * ������ȡ�ֻ���������CTWAP��CTNET��ʱ��PDSN������ֻ��ն˵�ԴIP��ַ�� 
	 *  
	 * @return 
	 * @author SHANHY 
	 */  
//	public static String getPsdnIp() {  
//	    try {  
//	        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {  
//	            NetworkInterface intf = en.nextElement();  
//	            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {  
//	                InetAddress inetAddress = enumIpAddr.nextElement();  
//	                if (!inetAddress.isLoopbackAddress() && inetAddress instanceof InetAddress) {  
//	                //if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet6Address) {  
//	                    return inetAddress.getHostAddress().toString();  
//	                }  
//	            }  
//	        }  
//	    } catch (Exception e) {  
//	    }  
//	    return "";  
//	}  

	public boolean isNetworkConnected(Context context) {
		if (context != null) {
			ConnectivityManager mConnectivityManager = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo mNetworkInfo = mConnectivityManager
					.getActiveNetworkInfo();
			if (mNetworkInfo != null) {
				return mNetworkInfo.isAvailable();
			}
		}
		return false;
	}
	
	/**
	 * ��֤���������Ƿ�Ϸ�
	 * 
	 * @param strEmail
	 * @return
	 */
	public boolean isEmail(String strEmail) {
		// String strPattern =
		// "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
		String strPattern = "^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$";

		Pattern p = Pattern.compile(strPattern);
		Matcher m = p.matcher(strEmail);
		return m.matches();
	}
	

	/**
	 * ��֤�Ƿ����ֻ����
	 * 
	 * @param str
	 * @return
	 */
	public boolean isMobile(String str) {
		Pattern pattern = Pattern.compile("1[0-9]{10}");
		Matcher matcher = pattern.matcher(str);
		if (matcher.matches()) {
			return true;
		} else {
			return false;
		}
	}
	

}